package com.prs.crm.action.doc;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.Util;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.doc.DocSendWork;
import com.prs.crm.domain.doc.DocTaskSend;
import com.prs.crm.domain.doc.Document;
import com.prs.crm.domain.doc.DocumentTaskType;
import com.prs.crm.service.doc.DocSendWorkService;
import com.prs.crm.service.doc.DocTaskSendService;
import com.prs.crm.service.doc.DocumentService;
import com.prs.crm.service.doc.DocumentTaskTypeService;
import com.prs.crm.service.oa.ApplyService;

/**
 * 任务文档
 * 
 * @author 兰小刚
 * 
 */
public class DocumentTaskAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private static final String key = "com.prs.crm.oa.Document";

	private Date startTime;

	private String dept;
	
	private Date endTime;

	private String DocIds;

	private String[] docNames;

	private String[] docCodes;

	private String[] dockeyWords;

	private Attachment[] attachs;

	private String receivePersons;

	private Document docuemnt;

	private DocSendWork docSendWork;

	private DocTaskSend docTaskSend;
	
	private DocumentTaskType documentTaskType;
	
	private DocumentTaskTypeService documentTaskTypeService;

	private DocSendWorkService docSendWorkService;

	private ApplyService applyService;

	private DocTaskSendService docTaskSendService;

	private DocumentService documentService;

	private Integer[] ids;

	private String docids;

	private String testV;

	private Collection<DocSendWork> reDocs;

	private Collection<Document> docidds;

	private Collection<DocSendWork> slist;

	private PaginationSupport pagination;
	
	private TreeNode root;

	@Action("listTaskSend")
	public String listTaskSend() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Document.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.addOrder(Order.desc("recordTime"));
		this.setPagination(this.getDocumentService().listByPage(criteria,
				pagination));
		return SUCCESS;

	}
	@Action("listDocument")
	public String listDocument() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Document.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		 if(dept != null){
				if(!"all".equals(dept)){
					String t = dept;
					String ydmc = dept.split(",")[0];
					if(ToolKits.isInteger(ydmc)){
						criteria.createAlias("recorder", "re");
						criteria.add(Restrictions.eq("re.id", Integer.parseInt(ydmc)));
					}else if(ydmc.indexOf("dept") != -1){
						Collection<Integer> pes = new ArrayList<Integer>();
						for(String pe : t.split(",")){
							if(pe.indexOf("dept") == -1){
								pes.add(Integer.parseInt(pe));
							}
						}
						if(pes.size() > 0){
							criteria.createAlias("recorder", "re");
							criteria.add(Restrictions.in("re.id", pes));
						}
					}
				}
		 }
		criteria.addOrder(Order.desc("recordTime"));
		this.setPagination(this.getDocumentService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	
	@Action(value = "submitSendTask", results = {
			@Result(name = "success", type = "chain", location = "editTaskSend"),
			@Result(name = "error", type = "chain", location = "listTaskSend") })
	public String submitDocument() {
		String allids = "";
		if (ids == null) {
			this.addActionError("请选择要发送的文档!");
			return ERROR;
		} else {
			if (reDocs == null) {
				reDocs = new ArrayList<DocSendWork>();
			} else {
				reDocs.clear();
			}
			String idss = "";
			int ji = 0;
			for (Integer zid : ids) {
				if (ji == (ids.length - 1)) {
					idss = idss + zid;
				} else {
					idss = idss + zid + ",";
				}
				Document documentObject = this.getDocumentService().get(zid);
				DocSendWork doc = new DocSendWork();
				doc.setId(null);
				doc.setDocName(documentObject.getName());
				doc.setDocCode(documentObject.getDocumentcode());
				doc.setDockeyWord(documentObject.getKeyWord());
				doc.setAttach(documentObject.getAttach());
				reDocs.add(doc);
				ji++;
			}
			docids = idss.trim();
			return SUCCESS;
		}
	}

	@Action("editTaskSend")
	public String editTaskSend() {

		return SUCCESS;
	}

	/** save ***/
	@Action(value = "submitTaskWord", results = {
			@Result(name = "success", type = "chain", location = "listTaskSend"),
			@Result(name = "error", type = "chain", location = "listTaskSend") })
	public String submitTaskWord() {

		this.getDocTaskSendService().submitDocTaskSend(startTime, endTime,
				DocIds, receivePersons, docNames, docCodes, dockeyWords,
				attachs);
		return SUCCESS;
	}

	/** 发放记录 **/
	@Action("listSendTask")
	public String listSendTask() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DocTaskSend.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDocTaskSendService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	/** 接收记录 **/
	@Action("listReceiveTask")
	public String listReceiveTask() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DocTaskSend.class);
		criteria.add(Restrictions.eq("receivePerson", AppContext
				.getAccountContext().currentUser()));
		criteria.add(Restrictions.le("startTime", Util.nowFullTime()));
		criteria.add(Restrictions.ge("endTime", Util.nowFullTime()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDocTaskSendService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewTaskWork")
	public String viewTaskWork() {
		docTaskSend = this.getDocTaskSendService().get(docTaskSend.getId());
		this.slist = docTaskSend.getDocSendWork();
		return SUCCESS;
	}

	
	@Action(value="loadDocumentTree",results={@Result(name="success",type="json",params={"root","root"})})
	public String loadTree() {
		root = new TreeNode();
		root.setId("-1");		
		TreeNode firstNode = new TreeNode();
		firstNode.setId("0");
		firstNode.setText("文档选择");	
		
		Collection<DocumentTaskType> depts = this.getDocumentTaskTypeService().getRootDocumentTaskType();
		for(DocumentTaskType  dept:depts){
			System.out.println("文档dept_name="+dept.getName());
		}
		
		for (DocumentTaskType depart : depts) {
			TreeNode node = new TreeNode();
			node.setText(depart.getName());
			node.setId("dept-"+depart.getId().toString());
			firstNode.addChildNode(node);
			addDepartNode(node, depart);
			addTypeNode(node,depart);
		}

		root.addChildNode(firstNode);
		return SUCCESS;
	}
	
	
	@Action("listAllDocument")
	public String listAllDocument() {
		return SUCCESS;
	}
	
	public Document getDocuemnt() {
		return docuemnt;
	}

	public void setDocuemnt(Document docuemnt) {
		this.docuemnt = docuemnt;
	}

	public DocumentService getDocumentService() {
		return documentService;
	}

	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public DocSendWork getDocSendWork() {
		return docSendWork;
	}

	public void setDocSendWork(DocSendWork docSendWork) {
		this.docSendWork = docSendWork;
	}

	public DocTaskSend getDocTaskSend() {
		return docTaskSend;
	}

	public void setDocTaskSend(DocTaskSend docTaskSend) {
		this.docTaskSend = docTaskSend;
	}

	public DocSendWorkService getDocSendWorkService() {
		return docSendWorkService;
	}

	public void setDocSendWorkService(DocSendWorkService docSendWorkService) {
		this.docSendWorkService = docSendWorkService;
	}

	public DocTaskSendService getDocTaskSendService() {
		return docTaskSendService;
	}

	public void setDocTaskSendService(DocTaskSendService docTaskSendService) {
		this.docTaskSendService = docTaskSendService;
	}

	public static String getKey() {
		return key;
	}

	public String getTestV() {
		return testV;
	}

	public void setTestV(String testV) {
		this.testV = testV;
	}

	public Collection<DocSendWork> getReDocs() {
		return reDocs;
	}

	public void setReDocs(Collection<DocSendWork> reDocs) {
		this.reDocs = reDocs;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getDocIds() {
		return DocIds;
	}

	public void setDocIds(String docIds) {
		DocIds = docIds;
	}

	public String getReceivePersons() {
		return receivePersons;
	}

	public void setReceivePersons(String receivePersons) {
		this.receivePersons = receivePersons;
	}

	public String getDocids() {
		return docids;
	}

	public void setDocids(String docids) {
		this.docids = docids;
	}

	public Collection<Document> getDocidds() {
		return docidds;
	}

	public void setDocidds(Collection<Document> docidds) {
		this.docidds = docidds;
	}

	public ApplyService getApplyService() {
		return applyService;
	}

	public void setApplyService(ApplyService applyService) {
		this.applyService = applyService;
	}

	public String[] getDocNames() {
		return docNames;
	}

	public void setDocNames(String[] docNames) {
		this.docNames = docNames;
	}

	public String[] getDocCodes() {
		return docCodes;
	}

	public void setDocCodes(String[] docCodes) {
		this.docCodes = docCodes;
	}

	public String[] getDockeyWords() {
		return dockeyWords;
	}

	public void setDockeyWords(String[] dockeyWords) {
		this.dockeyWords = dockeyWords;
	}

	public Attachment[] getAttachs() {
		return attachs;
	}

	public void setAttachs(Attachment[] attachs) {
		this.attachs = attachs;
	}

	public Collection<DocSendWork> getSlist() {
		return slist;
	}

	public void setSlist(Collection<DocSendWork> slist) {
		this.slist = slist;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public DocumentTaskType getDocumentTaskType() {
		return documentTaskType;
	}

	public void setDocumentTaskType(DocumentTaskType documentTaskType) {
		this.documentTaskType = documentTaskType;
	}

	public DocumentTaskTypeService getDocumentTaskTypeService() {
		return documentTaskTypeService;
	}

	public void setDocumentTaskTypeService(
			DocumentTaskTypeService documentTaskTypeService) {
		this.documentTaskTypeService = documentTaskTypeService;
	}
	private void addDepartNode(TreeNode parent, DocumentTaskType dept) {
		for (DocumentTaskType depart : dept.getChilds()) {
			TreeNode node = new TreeNode();
			node.setText(depart.getName());
			node.setId("dept-"+depart.getId().toString());
			parent.addChildNode(node);
			addDepartNode(node, depart);
			addTypeNode(node,depart);
		}
	}
	private void addTypeNode(TreeNode parent,DocumentTaskType dept)
	{
		for (DocumentTaskType person:this.getDocumentTaskTypeService().getAllDocumentTaskTypes())
		{
			TreeNode node = new TreeNode();
			if (person.getName()!=null)
				node.setText(person.getName());
			else
				node.setText(person.getName());
			node.setId(person.getId().toString());
			parent.addChildNode(node);
		}
	}
	public String getDept() {
		return dept;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}

}
